package org.wuliu.Controller;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import org.apache.commons.lang.StringUtils;
import org.apache.shiro.crypto.hash.Md5Hash;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.wuliu.dao.*;
import org.wuliu.pojo.*;
import org.wuliu.utils.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Controller
@RequestMapping("/user")
@CrossOrigin(value = "*")
public class UserController {
    @Autowired
    private QvpaiDao qvpaiDao;
    @Autowired
    private PermDao permDao;
    @Autowired
    private UserRoleDao userRoleDao;
    @Autowired
    private UserDao userDao;
    @RequestMapping("/list")
    @ResponseBody
    public ResponseResult getlist(@RequestParam(defaultValue = "1") int page, @RequestParam(defaultValue = "10") int limit, Qvpai qvpai,String zts,String gongs){
        if(StringUtils.isNotEmpty(zts)){
            qvpai.setZhuangtais(zts.split(","));
        }
        if(StringUtils.isNotEmpty(gongs)){
            qvpai.setDanweis(gongs.split(","));
        }
        Page<Qvpai> pag= PageHelper.startPage(page,limit);
        List<Qvpai> list=qvpaiDao.getList(qvpai);
        return new ResponseResult(pag.getTotal(),list);
    }
    @RequestMapping("/qvpaiadd")
    @ResponseBody
    public ResponseResult getlist(Qvpai qvpai){
        qvpaiDao.add(qvpai);
        return ResponseResult.createSuccessResUle();
    }
    @RequestMapping("/qvpaiupdate")
    @ResponseBody
    public ResponseResult updataqvpai(Qvpai qvpai){
        qvpaiDao.update(qvpai);
        return ResponseResult.createSuccessResUle();
    }
    @RequestMapping("/qpUp")
    @ResponseBody
    public ResponseResult qpUp(Integer zhuangtai,Integer id){
        qvpaiDao.shangren(zhuangtai,id);
        return ResponseResult.createSuccessResUle();
    }
    @RequestMapping("/deleqp")
    @ResponseBody
    public ResponseResult deleqp(String idstr){
        String[] ids=idstr.split(",");
        qvpaiDao.deleqp(ids);
        return ResponseResult.createSuccessResUle();
    }
    @RequestMapping("/qpfzr")
    @ResponseBody
    public ResponseResult getfuze(){
        List<Qvpai> list=qvpaiDao.getBynotDing();
        return new ResponseResult(list);
    }
    @RequestMapping("/show")
    @ResponseBody
    public ResponseResult getShow(@RequestParam(defaultValue = "1") int page, @RequestParam(defaultValue = "10") int limit,User user){
        Page<Qvpai> pag= PageHelper.startPage(page,limit);
        List<User> list=userDao.getList(user);
        return new ResponseResult(pag.getTotal(),list);
    }
    @RequestMapping("/add")
    @ResponseBody
    public ResponseResult getadd(User user,Integer roleId){
        String pwd=user.getPassword();
        user.setScal(pwd);
        String pass=new Md5Hash(pwd,pwd).toString();
        user.setPassword(pass);
        System.out.println(user.toString());
        userDao.add(user);
        UserRole userRole=new UserRole();
        userRole.setRoleId(roleId);
        userRole.setUserId(user.getId());
        userRoleDao.add(userRole);
        return ResponseResult.createSuccessResUle();
    }
    @RequestMapping("/login")
    @ResponseBody
    public ResponseResult login(String userCode,String password){
        Map<String,Object> map=new HashMap<>();
        User user=userDao.login(userCode);
        if(user==null){
            return new ResponseResult(1,"没有此账号");
        }else{
            String psg=password;
            psg=new Md5Hash(password,user.getScal()).toString();
            if(!psg.equals(user.getPassword())){
                return new ResponseResult(1,"密码错误");
            }
        }
        if(user.getZhuangtai()==0){
            return new ResponseResult(1,"账号已被封禁,原因:"+user.getBeizhu());
        }
        List<Perm> perms=permDao.getUserPerm(user.getId());
        map.put("user_login",user);
        map.put("user_perms",perms);
        ResponseResult<User> results=new ResponseResult(map);
        return results;
    }
    @RequestMapping("/upUser")
    @ResponseBody
    public ResponseResult upUser(User user,Integer roleId){
        String pwd=user.getPassword();
        user.setScal(pwd);
        String pass=new Md5Hash(pwd,pwd).toString();
        user.setPassword(pass);
        System.out.println(user.toString());
        userDao.update(user);
        UserRole userRole=new UserRole();
        userRole.setRoleId(roleId);
        userRole.setUserId(user.getId());
        userRoleDao.update(userRole);
        return ResponseResult.createSuccessResUle();
    }
    @RequestMapping("/batchdel")
    @ResponseBody
    public ResponseResult batchdel(String idstr){
        String[] ids=idstr.split(",");
        userDao.batchDel(ids);
        userRoleDao.batchDel(ids);
        return ResponseResult.createSuccessResUle();
    }
    @RequestMapping("/setZhuangtai")
    @ResponseBody
    public ResponseResult setZhuangtai(User user){
        userDao.upZhuangtai(user);
        return ResponseResult.createSuccessResUle();
    }
}
